//
//  LXDSingerTableViewCell.m
//  BLDMusics
//
//  Created by lxd on 16/3/22.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "LXDSingerTableViewCell.h"

@interface  LXDSingerTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

/**下面的collection*/
@property (nonatomic, retain) UICollectionView *collection;

@end

@implementation LXDSingerTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createControl];
    }
    return self;
}


- (void)setID:(NSNumber *)ID {
    _ID = ID;
}

#pragma mark - 创建collection
- (void)createControl {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
    [self.contentView addSubview:_collection];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.pagingEnabled = YES;
    _collection.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    _collection.backgroundColor = [UIColor clearColor];
    _collection.showsHorizontalScrollIndicator = NO;
    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collection.bounces = NO;
    
    // 给collection添加通知，通过点击button来改变collection的偏移量
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentOffset:) name:@"偏移量" object:nil];


    // 注册
    [_collection registerClass:[LXDSingleCollectionViewCell class] forCellWithReuseIdentifier:@"single"];
    [_collection registerClass:[LXDSpecialCollectionViewCell class] forCellWithReuseIdentifier:@"special"];
    [_collection registerClass:[LXDMVCollectionViewCell class] forCellWithReuseIdentifier:@"MV"];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundColor = [UIColor clearColor];
    _collection.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
}


#pragma mark - 点击button来改变collection的偏移量
- (void)changeContentOffset:(NSNotification *)notification {
    UIButton *button = notification.object;
    NSInteger tag = button.tag % 1000;
    _collection.contentOffset = CGPointMake(SCREEN_WIDTH * tag, 0);
}


#pragma mark - collection的偏移量改变时去改变button的位置
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"红色小条" object:_collection userInfo:nil];
}



#pragma mark - 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
#pragma mark 返回item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        LXDSingleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"single" forIndexPath:indexPath];
        cell.ID = _ID;
        return cell;
    }
    
        LXDMVCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MV" forIndexPath:indexPath];
        cell.ID = _ID;
        return cell;
   
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.contentView.frame.size.width, self.contentView.frame.size.height);
}







- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
